package com.yanqu.road.logic.xlogic.user;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.utils.date.DateHelper;

public class XUserBaseInfo {

    private long userId;
    private UserBaseInfo oldInfo;
    private long lastAllTime;

    public XUserBaseInfo(long userId) {
        this.userId = userId;
        this.oldInfo = null;
        this.lastAllTime = 0L;
    }

    public PlayerProto.PlayerBaseTempMsg.Builder getUpdateMsg(UserBaseInfo newInfo) {
        synchronized (this) {
            PlayerProto.PlayerBaseTempMsg.Builder builder;

            // 是否全量
            boolean isAll = oldInfo == null;
            if (!isAll) isAll = System.currentTimeMillis() - lastAllTime >= DateHelper.MINUTE_MILLIONS * 10;

            // 获取更新msg
            if (isAll) {
                builder = PlayerBasePb.parsePlayerBaseTempMsg(newInfo);
            } else {
                builder = PlayerBasePb.parseUpdatePlayerBaseTempMsg(oldInfo, newInfo);
            }

            // 修改旧值
            oldInfo = newInfo;
            lastAllTime = isAll ? System.currentTimeMillis() : lastAllTime;

            return builder;
        }
    }


    public void setLastAllTime(long lastAllTime) {
        synchronized (this) {
            this.lastAllTime = lastAllTime;
        }
    }

    // ===============================================

    public long getUserId() {
        return userId;
    }

    public UserBaseInfo getOldInfo() {
        return oldInfo;
    }

    public long getLastAllTime() {
        return lastAllTime;
    }

}
